
/**
 * Data Service
 * 数据接口、处理服务
 */

angular.module('JiangGanApp')
  .service('DataService', ['$http', '$q', '$interval', DataService]);

function DataService($http, $q, $interval) {

  var that = this;
  var rest_url = 'http://10.0.13.196:3001/api/';

  //调用GET接口，返回promise
  this.restGetApi = function (url_path){
    var defered = $q.defer();
    var url = rest_url + url_path;
    $http.get(url).success(function (data) {
      defered.resolve(data);
    })
    .error(function (err) {
      defered.reject(err);
    });
    return defered.promise;
  };
  //调用POST接口，返回promise
  this.restPostApi = function (url_path, param){
    var defered = $q.defer();
    var url = rest_url + url_path;
    $http({
      method: 'POST',
      url: url,
      data: param
    }).success(function (data) {
      defered.resolve(data);
    })
    .error(function (err) {
      defered.reject(err);
    });
    return defered.promise;
  };

    //调用delete接口，返回promise
  this.restDeleteApi = function (url_path, id){
    var defered = $q.defer();
    var url = rest_url + url_path + '/' +id;
    $http({
      method: 'DELETE',
      url: url,
      data:{}
    }).success(function (data) {
      defered.resolve(data);
    })
    .error(function (err) {
      defered.reject(err);
    });
    return defered.promise;
  };


}